local yizhen = fk.CreateSkill{
  name = "dajubei__yizhen",
  derived_piles = "$yizhen_pile",
}

Fk:loadTranslationTable{
  ["dajubei__yizhen"] = "疑阵",
  [":dajubei__yizhen"] = "出牌阶段限一次，你可摸X张牌并将X张牌暗置于武将牌上（X为2-你武将牌上的牌数）。以下时机你需弃置一张暗置牌："
  .."当你成为【杀】的目标时→若此牌为黑色，该【杀】无效且你对【杀】的使用者造成一点伤害；"
  .."准备阶段→若此牌为红色，你令一名角色摸两张牌并回复一点体力。",
  ["#dajubei__yizhen"] = "疑阵：可以摸%arg张牌并将%arg张牌扣于武将牌上",

  ["#dajubei__yizhen-put"] = "疑阵：将%arg张牌扣于武将牌上",
  ["#dajubei__yizhen-remove"] = "疑阵：移去一张暗置牌",
  ["#dajubei__yizhen-recover"] = "疑阵：令一名角色摸两张牌并回复一点体力",

  ["$yizhen_pile"] = "疑阵",

  ["$dajubei__yizhen1"] = "愚者定物，以疑决疑。",
  ["$dajubei__yizhen2"] = "踌躇未决，将计就计。",
}

yizhen:addEffect("active", {
  anim_type = "special",
  prompt = function (self, player)
    local n = 2 - #player:getPile("$yizhen_pile")
    return "#dajubei__yizhen:::" .. n
  end,
  can_use = function (self, player)
    return player:usedSkillTimes(yizhen.name, Player.HistoryPhase) < 1 and #player:getPile("$yizhen_pile") < 2
  end,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  on_use = function (self, room, effect)
    local player = effect.from
    local n = 2 - #player:getPile("$yizhen_pile")
    player:drawCards(n, yizhen.name)
    if player.dead or player:isNude() then return end
    local cards = room:askToCards(player, {
      min_num = n,
      max_num = n,
      skill_name = yizhen.name,
      cancelable = false,
      include_equip = true,
      prompt = "#dajubei__yizhen-put:::" .. n,
    })
    player:addToPile("$yizhen_pile", cards, false, yizhen.name)
  end,
})

yizhen:addEffect(fk.TargetConfirmed, {
  anim_type = "defensive",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(yizhen.name) and player == target
    and data.card.trueName == "slash" and #player:getPile("$yizhen_pile") > 0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local cards = player:getPile("$yizhen_pile")
    local card = room:askToChooseCard(player, {
      target = player,
      flag = { card_data = {{"$yizhen_pile", cards},} },
      skill_name = yizhen.name,
      prompt = "#dajubei__yizhen-remove",
    })
    event:setCostData(self, card)
    return true
  end,
  on_use = function (self, event, target, player, data)
    local card = event:getCostData(self) ---@type integer
    local room = player.room
    room:moveCardTo(card, Card.DiscardPile, player, fk.ReasonPutIntoDiscardPile, yizhen.name, "$yizhen_pile", true, player)

    if Fk:getCardById(card).color == Card.Black then
      if player:isAlive() then
        data.use:removeTarget(player)
      end
      if data.from:isAlive() then
        room:damage{
          damage = 1,
          to = data.from,
          from = player,
          skillName = yizhen.name,
        }
      end
    end
  end
})

yizhen:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(yizhen.name) and player == target
    and data.phase == Player.Start and #player:getPile("$yizhen_pile") > 0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local cards = player:getPile("$yizhen_pile")
    local card = room:askToChooseCard(player, {
      target = player,
      flag = { card_data = {{"$yizhen_pile", cards},} },
      skill_name = yizhen.name,
      prompt = "#dajubei__yizhen-remove",
    })
    event:setCostData(self, {cards = {card}})
    return true
  end,
  on_use = function (self, event, target, player, data)
    local cards = event:getCostData(self).cards
    if not cards then return end
    local room = player.room
    room:moveCardTo(cards, Card.DiscardPile, player, fk.ReasonPutIntoDiscardPile, yizhen.name, "$yizhen_pile", true, player)
    if Fk:getCardById(cards[1]).color == Card.Red then
      local tos = room:askToChoosePlayers(player, {
        targets = room:getAlivePlayers(false),
        min_num = 1,
        max_num = 1,
        skill_name = yizhen.name,
        prompt = "#dajubei__yizhen-recover",
        cancelable = false,
      })
      room:recover{
        num = 1,
        who = tos[1],
        recoverBy = player,
        skillName = yizhen.name,
      }
      if tos[1]:isAlive() then
        tos[1]:drawCards(2, yizhen.name)
      end
    end
  end
})

return yizhen